<?php

require_once "db.php";

$username = db_escape($_POST['username']);
$password = db_escape(md5($_POST['pass1']));
$repass = db_escape(md5($_POST['repass']));
$email = db_escape($_POST['email']);
$genre1 = db_escape($_POST['genre1']);
$genre2 = db_escape($_POST['genre2']);
$genre3 = db_escape($_POST['genre3']);
$exist_name="SELECT * FROM accounts WHERE username='$username'";
$match = db_query($exist_name);
$numRows = mysqli_num_rows($match);
if ($numRows!=0) {
	echo 'This name already exist!';
}
else {
	if ($password == $repass) {
		$query = "INSERT INTO accounts (username, password, mail, type, favgenre1, favgenre2, favgenre3, active_or_not) 
		VALUES ('$username', '$password', '$email', 1 , '$genre1', '$genre2', '$genre3', 0)";
		$result = db_query($query);
		$subject = "Successfull registration in Watch :)";
		$message = "Congratilations! You have just registered in watch.com \r\n with username ".$username." and password ".$_POST['pass1'].".\r\n Now you can reservate seats.";
		mail($email, $subject, $message);
		$response = array("success" => "1", "mess" => "Registration Successfull!");
		echo json_encode($response);
    }
   else {
	   	echo 'Passwords does not match!';	
   }
}
	